package cn.tqfeiyang.smartadmin.system.controller;

import cn.tqfeiyang.smartadmin.commons.pagination.QueryParams;
import cn.tqfeiyang.smartadmin.system.response.RoleQueryVO;
import cn.tqfeiyang.smartadmin.system.service.UserService;
import cn.tqfeiyang.smartadmin.system.request.UserCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.UserUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.UserQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.UserQueryVO;
import cn.tqfeiyang.smartadmin.system.response.UserDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.Set;

/**
 * 用户控制器
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@RestController
@Validated
@RequestMapping("/api/system/user")
@Tag(name = "系统/用户")
public class UserController {

    /**
     * 用户服务
     */
    @Resource
    private UserService userService;

    /**
     * 查询用户
     *
     * @param dto 请求数据
     * @return 用户列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询用户")
    @SaCheckPermission("system:user:query")
    public ApiResult<PageData<UserQueryVO>> queryUsers(@Validated UserQueryDTO dto) {
        return ApiResult.success(userService.queryUsers(dto));
    }

    /**
     * 获取用户
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @OperationLog
    @GetMapping("/{userId}/read")
    @Operation(summary = "获取用户")
    @SaCheckPermission("system:user:read")
    public ApiResult<UserDetailVO> readUser(@PathVariable @EntityId Long userId) {
        return ApiResult.success(userService.readUser(userId));
    }

    /**
     * 添加用户
     *
     * @param dto 请求数据
     * @return 用户信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加用户")
    @SaCheckPermission("system:user:create")
    public ApiResult<UserDetailVO> createUser(@RequestBody @Validated UserCreateDTO dto) {
        Long userId = userService.createUser(dto);
        return ApiResult.success(userService.readUser(userId));
    }

    /**
     * 修改用户
     *
     * @param userId 用户id
     * @param dto 请求数据
     * @return 用户信息
     */
    @OperationLog
    @PostMapping("/{userId}/update")
    @Operation(summary = "修改用户")
    @SaCheckPermission("system:user:update")
    public ApiResult<UserDetailVO> updateUser(@PathVariable @EntityId Long userId, @RequestBody @Validated UserUpdateDTO dto) {
        userService.updateUser(userId, dto);
        return ApiResult.success(userService.readUser(userId));
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     */
    @OperationLog
    @PostMapping("/{userId}/delete")
    @Operation(summary = "删除用户")
    @SaCheckPermission("system:user:delete")
    public ApiResult<String> deleteUser(@PathVariable @EntityId Long userId) {
        userService.deleteUser(userId);
        return ApiResult.success();
    }

    /**
     * 查询角色
     *
     * @param userId 用户id
     */
    @OperationLog
    @GetMapping("/{userId}/roles/query")
    @Operation(summary = "查询角色")
    @SaCheckPermission("system:user:roles:query")
    public ApiResult<PageData<RoleQueryVO>> queryRoles(@PathVariable @EntityId Long userId, @Validated QueryParams dto) {
        return ApiResult.success(userService.queryRoles(userId, dto));
    }

    /**
     * 修改角色
     *
     * @param userId 用户id
     * @param roleIds 角色id集合
     */
    @OperationLog
    @PostMapping("/{userId}/roles/update")
    @Operation(summary = "修改角色")
    @SaCheckPermission("system:user:roles:update")
    public ApiResult<String> updateRoles(@PathVariable @EntityId Long userId, @RequestBody Set<Long> roleIds) {
        userService.updateRoles(userId, roleIds);
        return ApiResult.success();
    }
}
